<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addBtnFn(1)">添加权限</el-button>
      <el-table default-expand-all :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" size="mini" type="text" @click="addBtnFn(2,row.id)">添加</el-button>
            <el-button size="mini" type="text" @click="addBtnFn(3,row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加弹层 -->
      <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-form ref="form" label-width="80px" :model="form" :rules="rules">
          <el-form-item label="权限名称">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="form.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="form.description" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch v-model="form.enVisible" active-value="1" inactive-value="0" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="comfirmBtnFn">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { addPermission, delPermission, editPermission, getPermissionDetail, getPermissionList } from '@/api/permission'
import { translateDataToTree } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      dialogVisible: false,
      title: '',
      isEdit: false,
      form: {
        type: '',
        name: '',
        code: '',
        description: '',
        enVisible: 0,
        pid: 0
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '权限描述不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await getPermissionList()
      this.list = translateDataToTree(res, 0)
    },
    titleFn(type, id) {
      if (type === 1) {
        this.form.type = 1
        this.title = '添加权限'
        return
      }
      if (type === 2) {
        this.form.type = 2
        this.form.pid = id
        this.title = '添加子权限'
        return
      }
      if (type === 3) {
        this.title = '编辑权限'
      }
    },
    // 点击添加按钮
    async addBtnFn(type, id) {
      this.titleFn(type, id)
      if (type === 3) {
        const data = await getPermissionDetail(id)
        this.form = data
        this.isEdit = true
      }
      this.dialogVisible = true
    },
    // 弹层确认按钮
    async comfirmBtnFn() {
      await this.$refs.form.validate()
      if (this.isEdit) {
        this.isEdit = false
        await editPermission(this.form)
        this.$message.success('修改成功')
      } else {
        await addPermission(this.form)
        this.$message.success('添加成功')
      }
      this.handleClose()
      this.getList()
    },
    // 取消
    handleClose() {
      this.dialogVisible = false
      this.$refs.form.resetFields() // 重置表单
      this.form = {} // 清空对象
    },
    // 删除
    async delFn(id) {
      await delPermission(id)
      this.$message.success('删除成功')
      this.getList()
    },
    // 编辑功能
    async editFn(data) {
      const res = await getPermissionDetail(data.id)
      console.log(res)
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
